package id.co.shuba.spamobile.list;

import id.co.shuba.spamobile.Sessions;
import id.co.shuba.spamobile.SimpleCursorLoader;
import id.co.shuba.spamobile.data.ISharedDB;
import id.co.shuba.spamobile.data.MaintenanceVisitDB;
import id.co.shuba.spamobile.widget.MaintenanceVisitCursorAdapter;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ListView;

public class MaintenanceVisitListFragment extends ListFragment implements
		LoaderManager.LoaderCallbacks<Cursor> {
	
	private String _aoCode;
	private String _filter;
	private String _sort;
	private int _type;
	
	public interface OnMaintenanceVisitSelectedListener {
        public void onMaintenanceVisitSelected(String id);
    }		
	
	private OnMaintenanceVisitSelectedListener _listener;	
	
	private MaintenanceVisitDB _db;
	private MaintenanceVisitCursorAdapter _adapter;	
	
	public void setOnMaintenanceVisitSelectedListener(OnMaintenanceVisitSelectedListener listener) {
		_listener = listener;
	}

	public static final class MaintenanceVisitCursorLoader extends SimpleCursorLoader {					
		private final String _aoCode;
		private final String _filter;		
		private final int _type; 
		private final String _sort; 
		private final MaintenanceVisitDB _helper;			

		public MaintenanceVisitCursorLoader(Context context, MaintenanceVisitDB helper,
				String aoCode, String filter, int type, String sort) {
			super(context);		
			_helper = helper;	
			_aoCode = aoCode;
			_filter = filter;
			_type = type;
			_sort = sort;
		}			

		@Override
		public Cursor loadInBackground() {						
			return _helper.getList(_aoCode, _filter, _sort);
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		if (_listener == null)
			return;

		Cursor cursor = _adapter.getCursor();
		cursor.moveToPosition(position);
		
		_listener.onMaintenanceVisitSelected(cursor.getString(cursor.getColumnIndex("VISIT_ID")));		
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
				
		_adapter = new MaintenanceVisitCursorAdapter(getActivity(), null, true);             		
        	
		setListAdapter(_adapter);		
	}	
	
	public void setAoCode(String aoCode) {
		_aoCode = aoCode;
	}

	public void setFilter(String filter) {
		_filter = filter;				
	}	
	
	public void setType(int type) {
		_type = type;		
	}
	
	public void setSort(String sort) {
		_sort = sort;		
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		
		_db = new MaintenanceVisitDB(((ISharedDB)getActivity()).getDB());
		
        getLoaderManager().initLoader(0, null, this);
	}
	
	public void reload() {		
		Bundle bundle = new Bundle();
		bundle.putString("aoCode", _aoCode);
		bundle.putString("filter", _filter);
		bundle.putInt("type", _type);
		bundle.putString("sort", _sort);		
		
	    getLoaderManager().restartLoader(0, bundle, this);
	}
	
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {	
		String aoCode = null;
		String filter = null;
		int type = 0;
		String sort = null;
		
		if (args != null) {
			aoCode = args.getString("aoCode");
			filter = args.getString("filter");
			type = args.getInt("type");
			sort = args.getString("sort");
		}
		
		return new MaintenanceVisitCursorLoader(getActivity(), _db, aoCode, filter, type, sort);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		_adapter.swapCursor(cursor);
	}

	public void onLoaderReset(Loader<Cursor> loader) {
		_adapter.swapCursor(null);
	}
}
